#define DEBUG
#include <cstdio>
#define MAXN 1000
#define MAXB 20

using namespace std;

int min(int a, int b) {
  return a<b ? a : b;
}

int max(int a, int b) {
  return a>b ? a : b;
}

int solve(int o, int n, int b, int cow[][MAXB+1], int barn[], int minx, int maxx) {
  if (o>n) {
    return maxx-minx+1;
  } 
  int ret=b;
  for (int i=1; i<=b; i++) {
    if (barn[cow[o][i]]>0) {
      barn[cow[o][i]]--;
      ret=min(ret, solve(o+1, n, b, cow, barn, min(minx, i), max(maxx, i)));
      barn[cow[o][i]]++;
    }
  }
  return ret;
}

int main() {
#ifdef DEBUG
  freopen("2.in", "r", stdin);
  freopen("2.out", "w", stdout);
#endif

  static int cow[MAXN+1][MAXB+1];
  static int barn[MAXB+1];
  int n, b;
  scanf("%d %d", &n, &b);
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=b; j++) {
      scanf("%d", cow[i]+j);
    }
  }
  for (int i=1; i<=b; i++) {
    scanf("%d", barn+i);
  }

  printf("%d", solve(1, n, b, cow, barn, b, 1));

  fclose(stdin);
  fclose(stdout);
  return 0;
}
